package com.ybt.service.endpoint;

import com.ruleengine.context.IWorkflowConext;
import com.ruleengine.endpoint.IEndpoint;
import com.ruleengine.endpoint.callback.IEndpointCallBack;
import com.ruleengine.endpoint.data.IEndpointData;
import com.ruleengine.endpoint.exception.EndpointException;
import com.ybt.service.IService;
import com.ybt.service.callback.IServiceCallback;
import com.ybt.service.exception.ServiceException;

/**
 * 银保通本地服务的endpoint适配器.
 * 
 * @author wuxiao
 *
 * Created on Jul 22, 2012
 */
public class BeanEndpoint implements IEndpoint {

    /**
     * 本地服务名称
     */
    private String target;
    /**
     * 本地服务对象
     */
    private IService service;

    public IEndpointData send(IWorkflowConext context, IEndpointData data)
            throws EndpointException {
        // TODO Auto-generated method stub
        try {
            service.service(context, data);
            return data;
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            throw new EndpointException(e);
        }
    }

    public void sendNonBlocking(IWorkflowConext context,
            IEndpointData data, final IEndpointCallBack callback)
            throws EndpointException {
        // TODO Auto-generated method stub
        try {
            service.serviceNonBlocking(context, data, new IServiceCallback(){

                public void onComplete(IWorkflowConext context,
                        IEndpointData data) {
                    // TODO Auto-generated method stub
                    //bean调用数据都通过IEndpointData交换，丢弃data
                    //调用回调方法
                    callback.onComplete(context, data);
                }

                public void onError(Exception cause) {
                    // TODO Auto-generated method stub
                    // 异步调用协议返回失败
                    callback.onError(cause);
                }
                
            });
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            throw new EndpointException(e);
        }
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public IService getService() {
        return service;
    }

    public void setService(IService service) {
        this.service = service;
    }

}
